package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsBrandMapper;
import com.shop.pojo.DtsBrand;
import com.shop.service.DtsBrandService;
import com.shop.service.DtsGoodsService;
import com.shop.vo.DtsBrandVo;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName DtsBrandServiceImpl
 * @Author xiao宋
 * @Date 19:19  2022/10/16
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsBrandServiceImpl implements DtsBrandService {
    @Autowired
    DtsBrandMapper brandMapper;
    @Autowired
    DtsGoodsService goodsService;

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String name, Integer id) {

        // 正好有个 DtsUser 实体类
        IPage<DtsBrand> iPage = new Page<>(page, limit);
        QueryWrapper<DtsBrand> w = new QueryWrapper<>();
        w.eq("deleted", false);
//        前端显示的品牌商名称 name
        if (!StringUtils.isEmpty(name)) {
            w.eq("name", name);
        }
        if (id != null) {
            w.eq("id", id);
        }
//        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }

        brandMapper.selectPage(iPage, w);

        Map<String, Object> result = new HashMap<>();
        result.put("total", iPage.getTotal());
        result.put("items", iPage.getRecords());
        return result;
    }

    @Override
    public void delete(DtsBrand brand) {
        LambdaQueryWrapper<DtsBrand> w = new LambdaQueryWrapper<>();
        w.eq(DtsBrand::getId, brand.getId());
        int count = goodsService.findValidGoodsByBrandId(brand.getId());
        if (count > 0) {
            throw new RuntimeException("该品牌商旗下还有商品未下架，所以不能删除！");
        }

        brand.setDeleted(true);
        brand.setUpdateTime(new Date());
        brandMapper.updateById(brand);

    }

    @Override
    public void update(DtsBrand brand) {
        brand.setUpdateTime(new Date());
        brandMapper.updateById(brand);
    }


    @Override
    public List<Map<String, Object>> findBrandList() {
//        查没有逻辑删除的
        LambdaQueryWrapper<DtsBrand> w = new LambdaQueryWrapper<>();
        w.eq(DtsBrand::getDeleted, false);
        List<DtsBrand> brandList = brandMapper.selectList(w);
        return brandList.stream().map(brand -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", brand.getId());
            map.put("label", brand.getName());
            return map;
        }).collect(Collectors.toList());
    }

    @Override
    public DtsBrand findById(Integer brandId) {
        return brandMapper.selectById(brandId);
    }

    @Override
    public List<Integer> findBrandIdsByAdminId(Integer adminId) {
        LambdaQueryWrapper<DtsBrand> w = new LambdaQueryWrapper<>();
        w.eq(DtsBrand::getAdminId, adminId);
        w.eq(DtsBrand::getDeleted, false);
        List<DtsBrand> list = brandMapper.selectList(w);

        return list.stream().map(brand -> {
            return brand.getId();
        }).collect(Collectors.toList());
    }

    @Override
    public DtsBrand insert(Map<String, Object> categoryIds) {
        DtsBrand brand = new DtsBrand();
//        转换格式拿数据
        Integer adminId = (Integer) categoryIds.get("adminId");
        Integer defaultCategoryId = (Integer) categoryIds.get("defaultCategoryId");
        String desc = (String) categoryIds.get("desc");
        String name = (String) categoryIds.get("name");
        String s = (String) categoryIds.get("floorPrice");
        int i = Integer.parseInt(s);

        BigDecimal bigDecimal = new BigDecimal(i);
//        Integer[] categoryList = (Integer[]) categoryIds.get("categoryIds");
        List<Integer>categoryList= (List<Integer>) categoryIds.get("categoryIds");
        if (categoryList != null || categoryList.size() > 0) {
            brand.setDefaultCategoryId(categoryList.get(1));
            System.out.println(categoryList.get(1));
        }
//        装数据
        brand.setId(adminId);
        brand.setDeleted(false);
        brand.setDefaultCategoryId(defaultCategoryId);
        brand.setDesc(desc);
        brand.setName(name);
        brand.setFloorPrice(bigDecimal);

        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());


        QueryWrapper w = new QueryWrapper<>();
        w.eq("id", adminId);

        brandMapper.insert(brand);
        DtsBrand dtsBrand = brandMapper.selectOne(w);
        return dtsBrand;

    }
}
